package com.fx.ruankao.controller;

import com.fx.ruankao.entity.Permission;
import com.fx.ruankao.service.PermissionService;
import com.fx.ruankao.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 权限控制器 - 管理端
 */
@RestController
@RequestMapping("/api/admin/permissions")
public class PermissionController {
    
    @Autowired
    private PermissionService permissionService;
    
    /**
     * 新增权限
     */
    @PostMapping
    public Result insert(@RequestBody Permission permission) {
        try {
            int result = permissionService.insert(permission);
            if (result > 0) {
                return Result.success("新增权限成功");
            } else {
                return Result.error("新增权限失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID删除权限
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            int result = permissionService.deleteById(id);
            if (result > 0) {
                return Result.success("删除权限成功");
            } else {
                return Result.error("删除权限失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 更新权限
     */
    @PutMapping
    public Result update(@RequestBody Permission permission) {
        try {
            int result = permissionService.update(permission);
            if (result > 0) {
                return Result.success("更新权限成功");
            } else {
                return Result.error("更新权限失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询权限
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        try {
            Permission permission = permissionService.selectById(id);
            if (permission != null) {
                return Result.success(permission);
            } else {
                return Result.error("未找到权限");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 查询所有权限
     */
    @GetMapping
    public Result selectAll() {
        try {
            List<Permission> permissions = permissionService.selectAll();
            return Result.success(permissions);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除权限
     */
    @DeleteMapping("/batch")
    public Result batchDelete(@RequestBody Integer[] ids) {
        try {
            int result = permissionService.batchDelete(ids);
            if (result > 0) {
                return Result.success("批量删除权限成功");
            } else {
                return Result.error("批量删除权限失败");
            }
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据角色ID查询权限列表
     */
    @GetMapping("/role/{roleId}")
    public Result selectByRoleId(@PathVariable Integer roleId) {
        try {
            List<Permission> permissions = permissionService.selectByRoleId(roleId);
            return Result.success(permissions);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 根据用户ID查询权限列表
     */
    @GetMapping("/user/{userId}")
    public Result selectByUserId(@PathVariable Integer userId) {
        try {
            List<Permission> permissions = permissionService.selectByUserId(userId);
            return Result.success(permissions);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    
    /**
     * 查询权限树
     */
    @GetMapping("/tree")
    public Result selectPermissionTree() {
        try {
            List<Permission> permissionTree = permissionService.selectPermissionTree();
            return Result.success(permissionTree);
        } catch (Exception e) {
            return Result.error("系统异常：" + e.getMessage());
        }
    }
    

}